package com.dengbw.systemtransfer.service;

import com.dengbw.systemtransfer.global.Column;
import com.dengbw.systemtransfer.mapper.PstFarmerMapper;
import com.dengbw.systemtransfer.mapper.SysOrgMapper;
import com.dengbw.systemtransfer.mapper.SysUserMapper;
import com.dengbw.systemtransfer.model.db.PstFarmer;
import com.dengbw.systemtransfer.model.db.SysOrg;
import com.dengbw.systemtransfer.model.db.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author dengbw
 * @date 2021/11/11 2:34 下午
 */
@Service
public class PstFarmerService {

    PstFarmerMapper pstFarmerMapper;
    SysUserMapper sysUserMapper;
    SysOrgMapper sysOrgMapper;

    @Autowired
    public PstFarmerService(PstFarmerMapper pstFarmerMapper, SysUserMapper sysUserMapper, SysOrgMapper sysOrgMapper) {
        this.pstFarmerMapper = pstFarmerMapper;
        this.sysUserMapper = sysUserMapper;
        this.sysOrgMapper = sysOrgMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    public void addToFarmer(String zhenCorpId, Long subId) {
        List<PstFarmer> existFarmers = pstFarmerMapper.queryList(v -> v.eq(Column.SUB_ID, subId));
        List<SysUser> existUsers = sysUserMapper.queryList(v -> v.eq(Column.SUB_ID, subId));
        if (!CollectionUtils.isEmpty(existFarmers)) {
            List<Long> ids = existFarmers.stream().map(PstFarmer::getUserId).collect(Collectors.toList());
            List<SysUser> duplicatedUser = existUsers.stream().filter(u -> ids.contains(u.getUserId())).collect(Collectors.toList());
            existUsers.removeAll(duplicatedUser);
        }
        List<SysOrg> orgs = sysOrgMapper.queryList(v -> v.eq(Column.SUB_ID, subId));
        Map<String, String> orgIdMap = new HashMap<>();
        orgs.forEach(o -> orgIdMap.put(o.getOrgName(), o.getPath()));
        existUsers.stream()
                .filter(u -> orgIdMap.get(u.getOrgName()) != null)
                .forEach(u -> {
                    PstFarmer farmer = new PstFarmer();
                    farmer.setUserName(u.getName());
                    farmer.setOrgGuid(u.getOrgGuid());
                    farmer.setOrgGuidPath(orgIdMap.get(u.getOrgName()));
                    farmer.setMobile(u.getMobile());
                    String[] namePaths = orgIdMap.get(u.getOrgName()).split("\\.");
                    String villageName = namePaths[1];
                    farmer.setVillageName(villageName);
                    if (namePaths.length == 4) {
                        farmer.setGroupName(namePaths[3]);
                    }
                    farmer.setFamilyType(1);
                    farmer.setFamilyNum(1);
                    farmer.setStatus(1);
                    farmer.setDingUserId(u.getDingUserId());
                    farmer.setUserId(u.getUserId());
                    farmer.setCreateTime(u.getCreateTime());
                    farmer.setTenantId(zhenCorpId);
                    if (u.getUserType() == 1 || u.getUserType() == 2) {
                        farmer.setCadre(1);
                        farmer.setCadreDuty(9);
                    } else {
                        farmer.setCadre(0);
                    }
                    farmer.setIsHouseholder(1);
                    farmer.setSubTenantId(subId);
                    pstFarmerMapper.insert(farmer);
                });
    }
}
